"""
多进程锁, 多线程锁都要自己实现一遍
多进程通过Queue来实现进程通信

"""
# 线程锁
# from threading import Thread
# print("开始干活了！")
#
# def saodi(i):
#     print("我在扫地",i)
#
#
# if __name__ == "__main__":
#     process_array = []
#     for i in range(10):
#         p = Thread(target=saodi,args=(i,))
#         process_array.append(p)
#         p.start()
#     for p in process_array:
#         p.join()
#
# print("干活结束！")

# 互斥锁
# from threading import Thread, Lock
#
# print("开始干活了！")
# lock = Lock()
#
# def TuoDi(i):
#     with lock:
#         print("我在拖地", i)
#
#
# if __name__ == "__main__":
#     process_array = []
#     for i in range(10):
#         p = Thread(target=TuoDi, args=(i,))
#         process_array.append(p)
#         p.start()
#     for p in process_array:
#         p.join()
#
#     print("干活结束！")

# 消息队列Queue
from multiprocessing import Queue,Process

def save_to_queue(index, my_queue):
    my_queue.put(index)


if __name__ == "__main__":
    process_array = []
    my_queue = Queue()
    for i in range(10):
        p = Process(target=save_to_queue, args=(i, my_queue))
        process_array.append(p)
        p.start()
    for p in process_array:
        p.join()

    while True:
        print(my_queue.get())